package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.dtos.WemediaDto;
import com.heima.model.wemedia.pojos.Channel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.ChannelMapper;
import com.heima.wemedia.mapper.NewsMapper;
import com.heima.wemedia.service.ChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/11/29
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper,Channel> implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public ResponseResult find(ChannelDto dto) {
        LambdaQueryWrapper<Channel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName()!=null,Channel::getName,dto.getName());
        queryWrapper.orderByDesc(Channel::getCreatedTime);
        IPage<Channel> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<Channel> channelIPage = channelMapper.selectPage(page, queryWrapper);
        List<Channel> records = channelIPage.getRecords();
        return ResponseResult.okResult(records);
    }

    @Override
    public ResponseResult update(WemediaDto dto) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(dto,channel);
        if (dto.getStatus()){
            channel.setStatus((short)1);
        }else {
            LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmNews::getChannelId,dto.getId());
            List<WmNews> wmNews = newsMapper.selectList(queryWrapper);
            if (wmNews!=null && wmNews.size()>0){
                return ResponseResult.errorResult(500,"当前标题被使用");
            }
            channel.setStatus((short) 0);
        }
        channelMapper.updateById(channel);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult save(WemediaDto dto) {
        Channel channel = new Channel();
        BeanUtils.copyProperties(dto,channel);
        if (dto.getStatus()){
            channel.setStatus((short)1);
        }else {
            channel.setStatus((short) 0);
        }
        channel.setIsDefault((short) 1);
        channel.setCreatedTime(new Date());
        channelMapper.insert(channel);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult del(Integer id) {
        Channel channel = channelMapper.selectById(id);
        if (channel.getStatus()!=0){
            return ResponseResult.errorResult(500,"当前标签未禁用");
        }
        channelMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }
}
